package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.domain.dbo.MemberWechat;
import com.hornsun.domain.dbo.MemberWechatExample;
import com.hornsun.domain.dto.MemberWechatDTO;
import com.hornsun.repository.mapper.MemberWechatMapper;
import com.hornsun.util.LittleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/4/10.
 */
@Repository
public class MemberWechatRepository {

    @Resource
    private MemberWechatMapper memberWechatMapper;

    @Resource
    private LittleUtil littleUtil;

    public MemberWechat selectByOpenid(String openid){
        return memberWechatMapper.selectByPrimaryKey(openid);
    }

    public String getMemberGuidByOpenid(String openid){
        return memberWechatMapper.selectMemberGuidByOpenid(openid);
    }

    public boolean openidExist(String openid){
        MemberWechatExample memberWechatExample = new MemberWechatExample();
        MemberWechatExample.Criteria criteria = memberWechatExample.createCriteria();
        criteria.andOpenidEqualTo(openid);
        long count = memberWechatMapper.countByExample(memberWechatExample);
        if (count==1){
            return true;
        }else {
            return false;
        }
    }

    public void insertMemberWechat(MemberWechat memberWechat){
        int count = memberWechatMapper.insertSelective(memberWechat);
        if (count!=1){
            throw new IllegalArgumentException("insert memberWechat failed by memberWechat:"+memberWechat);
        }
    }

    public boolean hasGuidByOpenid(String openid){
        MemberWechatExample memberWechatExample = new MemberWechatExample();
        MemberWechatExample.Criteria criteria = memberWechatExample.createCriteria();
        criteria.andOpenidEqualTo(openid);
        long count = memberWechatMapper.countByExample(memberWechatExample);
        if (count==1){
            return true;
        }
        return false;
    }

    public void updateMemberWechat(MemberWechat memberWechat){
        int count = memberWechatMapper.updateByPrimaryKeySelective(memberWechat);
        if (count!=1){
            throw new IllegalArgumentException("update memberWechat failed by memberWechat:"+memberWechat);
        }
    }

    public long countByGuid(String guid){
        MemberWechatExample memberWechatExample = new MemberWechatExample();
        MemberWechatExample.Criteria criteria = memberWechatExample.createCriteria();
        criteria.andMemberGuidEqualTo(guid);
        return memberWechatMapper.countByExample(memberWechatExample);
    }

    public long totalCount(){
        return memberWechatMapper.countByExample(new MemberWechatExample());
    }

    public List<MemberWechat> getMembers(int page){
        return memberWechatMapper.selectByExampleWithRowbounds(new MemberWechatExample(),littleUtil.littleRowBounds(page));
    }

    public List<MemberWechatDTO> getMemberInfo(int page){
        int offset = (page-1)*12;
        return memberWechatMapper.getMemberInfo(offset);
    }

    public MemberWechat queryByOpenid(String openid){
        return memberWechatMapper.selectByPrimaryKey(openid);
    }



}
